package com.shengyijie.activity.tab.user;

import java.util.ArrayList;
 
 
import com.shengyijie.activity.MainActivity;
import com.shengyijie.activity.R;        
import com.shengyijie.adapter.viewadapter.GBListAdapter; 
import com.shengyijie.context.ContextApplication;
import com.shengyijie.model.http.HttpConnectApi;    
import com.shengyijie.model.object.baseobject.GuesstBook;
import com.shengyijie.model.object.baseobject.Item;    

import android.app.Activity;   
import android.content.Intent;   
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener; 
import android.widget.AdapterView; 
import android.widget.Button;
import android.widget.ImageButton; 
import android.widget.ListView; 
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener; 

public class EnterpriseGBActivity extends Activity implements OnClickListener{
   
	private ListView itemListView;
	private GBListAdapter listAdapter;
	private TextView tv_no;
	private View preView=null; 
	  
	
	  
	 private ImageButton tabHome;
	 private ImageButton tabCategory;
	 private ImageButton tabRecommended;
	 private ImageButton tabUser;
	 private ImageButton tabMore;
	
	public ArrayList<GuesstBook> itemList=new ArrayList<GuesstBook>(); ;
	
    public void onCreate(Bundle savedInstanceState){
         super.onCreate(savedInstanceState);
         requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); 
         setContentView(R.layout.itemlist);
         getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,      // 注意顺序
                 R.layout.title);
         initView();
          
         getListAdapter();
    }
     
    public void initView(){
    	
    	
    	TextView title=(TextView) this.findViewById(R.id.title);
		title.setText(ContextApplication.itemName); 
		
		Button back=(Button) this.findViewById(R.id.back);
		back.setVisibility(View.VISIBLE);
		back.setOnClickListener(this);
		
    	itemListView=(ListView) this.findViewById(R.id.categorylist); 
    	itemListView.setOnItemClickListener(itemClickListener);
    	tv_no=(TextView) this.findViewById(R.id.no);
    	tv_no.setVisibility(View.INVISIBLE); 
    	
    	tabHome=(ImageButton) this.findViewById(R.id.home);
    	tabCategory=(ImageButton) this.findViewById(R.id.category);
    	tabRecommended=(ImageButton) this.findViewById(R.id.recommended);
    	tabUser=(ImageButton) this.findViewById(R.id.user);
    	tabMore=(ImageButton) this.findViewById(R.id.more);
		try{
		    switch(ContextApplication.prePage){
		       case ContextApplication.PAGE_HOME:
		    	   tabHome.setSelected(true);
			       break;
		       case ContextApplication.PAGE_CATEGORY:
		    	   tabCategory.setSelected(true);
			       break;
		       case ContextApplication.PAGE_RECOMMENDED:
		    	   tabRecommended.setSelected(true);
			       break;
		       case ContextApplication.PAGE_USER:
		    	   tabUser.setSelected(true);
			       break;
		       case ContextApplication.PAGE_MORE:
		    	   tabMore.setSelected(true);
			       break;
		     }
		 }catch(Exception e){}
		
		 tabHome.setOnClickListener(this);
		 tabCategory.setOnClickListener(this);
		 tabRecommended.setOnClickListener(this);
		 tabUser.setOnClickListener(this);
		 tabMore.setOnClickListener(this);
		 
		 
    }
    
     
    
    public OnItemClickListener itemClickListener = new OnItemClickListener() {
		public void onItemClick(AdapterView<?> parent, View view,
				int position, long id) {
			try{
				 preView=view;  
//				 itemList.get(position).setStatus(1);
				 TextView status=(TextView) view.findViewById(R.id.status);
                 status.setText("已读");
//                 listAdapter.notifyDataSetChanged();
				 ContextApplication.guesstBook=ContextApplication.guesstBookList.getItem(position);
                 Intent i=new Intent();
                 i.setClass(EnterpriseGBActivity.this, GBDetailActivity.class);
                 EnterpriseGBActivity.this.startActivityForResult(i, 1);
                 
                 
				 //changeListViewApperence(true,view);   
				 
			}catch(Exception e){}
		}
    };
    
//    public void changeListViewApperence(boolean change,View view){
//		  try{
//			if(change){  
//		            TextView title=(TextView) view.findViewById(R.id.name);
//		            title.setTextColor(0xffffffff); 
//			}else{ 
//           	        TextView title=(TextView) preView.findViewById(R.id.name);
//		             title.setTextColor(0xff8D2124); 
//			}
//		  }catch(Exception e){}
//		}
//    private static boolean isFirstStart=true;
//    protected void onResume() {
//	      super.onResume();
//	      try{
//	    	  if(!isFirstStart){ 
//			      changeListViewApperence(false,preView);
//	    	  }
//	    	  isFirstStart=false; 
//	      }catch(Exception e){}
//	 }

	@Override
	public void onClick(View v) { 
		Bundle bd = new Bundle();
        bd.putString("state", "exit");	
        Intent it = new Intent();
        it.putExtras(bd);
		switch(v.getId()){
		     case R.id.home:
			    try{
				   if(ContextApplication.prePage!=ContextApplication.PAGE_HOME){
					   MainActivity.tabhost.setCurrentTab(0);
					   setResult(RESULT_OK, it); 
					   this.finish();
				   }
			    }catch(Exception e){}
			    break;
		     case R.id.category:
				    try{
					   if(ContextApplication.prePage!=ContextApplication.PAGE_CATEGORY){
						   MainActivity.tabhost.setCurrentTab(1);
						   setResult(RESULT_OK, it); 
						   this.finish();
					   }
				    }catch(Exception e){}
				    break;
		     case R.id.recommended:
				    try{
					   if(ContextApplication.prePage!=ContextApplication.PAGE_CATEGORY){
						   MainActivity.tabhost.setCurrentTab(2);
						   setResult(RESULT_OK, it); 
						   this.finish();
					   }
				    }catch(Exception e){}
				    break;
		     case R.id.user:
				    try{
					   if(ContextApplication.prePage!=ContextApplication.PAGE_USER){
						   MainActivity.tabhost.setCurrentTab(3);
						   setResult(RESULT_OK, it); 
						   this.finish();
					   }
				    }catch(Exception e){}
				    break;
		     case R.id.more:
				    try{
					   if(ContextApplication.prePage!=ContextApplication.PAGE_MORE){
						   MainActivity.tabhost.setCurrentTab(4);
						   setResult(RESULT_OK, it); 
						   this.finish();
					   }
				    }catch(Exception e){}
				    break;
				    
		     case R.id.back:
				    try{
					  this.finish();
				    }catch(Exception e){}
				    break;
		}
	}
	
	 public void changeToItemActivity(){
		 Intent intent=new Intent();
		 intent.setClass(this, EnterpriseGBActivity.class);
		 this.startActivityForResult(intent,1);
	}
	 
 
	  
	 
	 public void getListAdapter(){   
		 for(int i=0;i<ContextApplication.guesstBookList.getCount();i++){ 
			itemList.add(ContextApplication.guesstBookList.getItem(i));
		 }  
		 listAdapter = new GBListAdapter(this, itemList, itemListView);   
		 itemListView.setAdapter(listAdapter);
	}
	 
	 
	 
	 
	 protected void onActivityResult(int requestCode, int resultCode, Intent data) {		
		   if(requestCode==1){
				if (resultCode != RESULT_OK) {
					return;
				}
			   try{
			       String state = data.getStringExtra("state");
			       if(state.equals("exit")){
			    	   Bundle bd = new Bundle();
			           bd.putString("state", "exit");	
			           Intent it = new Intent();
			           it.putExtras(bd);
			           setResult(RESULT_OK, it); 
			    	   this.finish();
			       }
			   }
			   catch(Exception e){}
		   }
	 }
	 
     
    
}